<?php
class Page{
	protected $pageCount;		//总页数
	protected $totalRows;		//总行数
	protected $perPage;			//每页行数

	protected  $curPage;			//当前页号
	protected  $params;			//参数

	protected $varName = 'p';		//分页参数变量名

	public function __construct($totalRows=0, $curPage=1, $perPage=10, $params=array(), $varName='p'){
		$this->totalRows = is_numeric($totalRows) ? $totalRows : 0;
		$this->perPage   = (is_numeric($perPage) && $perPage>0) ? $perPage : 10;
		$this->pageCount = ceil($this->totalRows / $this->perPage);
		$this->curPage 	 = (is_numeric($curPage) && $curPage>0) ? $curPage : 1;
		$this->curPage   = $this->curPage > $this->pageCount ? $this->pageCount : $this->curPage;
		$this->varName = $varName;
		if (count($params)==0 && count($_GET)>1) {
			$this->params = $_GET;
			unset($this->params[$varName]);
			unset($this->params['s']);
			unset($this->params['c']);
			unset($this->params['m']);
			unset($this->params['l']);
			foreach ($this->params as $key => &$value) {
				if (empty($value)) {
					unset($this->params[$key]);
				}else{
					if ($key != htmlspecialchars($key) ) {
						$this->params[htmlspecialchars($key)] = htmlspecialchars($value);
						unset($this->params[$key]);
					}
					else{
						$value = htmlspecialchars($value);
					}
				}
			}
		}
	}

	public function getOffset(){
		return ($this->curPage-1)*$this->perPage;
	}

	public function show($theme='theme1.php'){
		$str = include($theme);
		return $str;
	}
}
?>

